package com.qf.servlet;

import com.qf.entity.User;
import com.qf.service.UserService;
import com.qf.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

/**
 * --- 缘分让我们相遇乱世以外 ---
 *
 * @author SJY
 * @date 2024/11/8
 * @desc
 */
@WebServlet("/regist")
public class RegistServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // req.setCharacterEncoding("utf-8");
        // 接收数据
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");
        String sex = req.getParameter("sex");

        // ORM封装数据
        User user = new User( );
        user.setPassword(password);
        user.setUsername(username);
        user.setPhone(phone);
        user.setSex(Integer.parseInt(sex));
        user.setCreateTime(new Date(  ));

        // 需要业务层完成注册的业务
        boolean isok = userService.regirst(user);
        // resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter( );
        if (isok) {
            req.getRequestDispatcher("/loginv1.html").forward(req,resp);
        } else {
            // out.write("<html>");
            // out.write("<head>");
            // out.write("<title>提示</title>");
            // out.write("</head>");
            // out.write("<body>");
            // out.write("<h1>注册失败</h1>");
            // out.write("</body>");
            // out.write("</html>");
            req.getRequestDispatcher("/error.jsp").forward(req,resp);
        }
    }
}
